package com.k.kc.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtil {
    // 2023-11-02T16:35:02+0800
    public static final String PATTERN_SSZ = "yyyy-MM-dd'T'HH:mm:ssZ";

    // 2023-11-02T16:35:02+08:00
    public static final String PATTERN_SSX = "yyyy-MM-dd'T'HH:mm:ssX";

    // 2023-11-02 16:35:45
    public static final String PATTERN_SS = "yyyy-MM-dd HH:mm:ss";

    // 2023-11-02 16:35:45:102
    public static final String PATTERN_SSS = "yyyy-MM-dd HH:mm:ss:SSS";


    public static Date stringToDate(String src, String srcPattern, ZoneId srcZoneId) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(srcPattern);
        sdf.setTimeZone(TimeZone.getTimeZone(srcZoneId));
        return sdf.parse(src);
    }
    public static Date nowDate() {
        return new Date();
    }

    public static String dateToStr(Date src, String destPattern, ZoneId destZoneId) {
        SimpleDateFormat sdf = new SimpleDateFormat(destPattern);
        sdf.setTimeZone(TimeZone.getTimeZone(destZoneId));
        return sdf.format(src);
    }

    public static String nowStr(String pattern,ZoneId zoneId){
        return dateToStr(nowDate(),pattern,zoneId);
    }

    public static String nowStrUTC(String pattern) {
        return nowStr(pattern, ZoneId.of("GMT"));
    }

    public static String nowStrLocal(String pattern) {
        return dateToStr(nowDate(), pattern, TimeZone.getDefault().toZoneId());
    }

    public static Date add(Date srcDate, int n, String flag) {
        if (n == 0) {
            return srcDate;
        }
        Calendar cal = Calendar.getInstance();
        //设置起时间
        cal.setTime(srcDate);
        if ("y".equalsIgnoreCase(flag)) {
            cal.add(Calendar.YEAR, n);
        } else if ("m".equalsIgnoreCase(flag)) {
            cal.add(Calendar.MONTH, n);
        } else if ("d".equalsIgnoreCase(flag)) {
            cal.add(Calendar.DATE, n);
        } else if ("h".equalsIgnoreCase(flag)) {
            cal.add(Calendar.HOUR, n);
        } else if ("mi".equalsIgnoreCase(flag)) {
            cal.add(Calendar.MINUTE, n);
        } else if ("s".equals(flag)) {
            cal.add(Calendar.SECOND, n);
        }
        return cal.getTime();
    }

    public static Date datePlusYear(Date src, int n) {
        return add(src, n, "y");
    }

    public static Date dateMinusYear(Date src, int n) {
        return add(src, ~n+1, "y");
    }

    public static Date datePlusMonth(Date src, int n) {
        return add(src, n, "m");
    }

    public static Date dateMinusMonth(Date src, int n) {
        return add(src, ~n+1, "m");
    }

    public static Date datePlusDay(Date src, int n) {
        return add(src, n, "d");
    }

    public static Date dateMinusDay(Date src, int n) {
        return add(src, ~n+1, "d");
    }

    public static Date datePlusHour(Date src, int n) {
        return add(src, n, "h");
    }

    public static Date dateMinusHour(Date src, int n) {
        return add(src, ~n+1, "h");
    }

    public static Date datePlusMinute(Date src, int n) {
        return add(src, n, "mi");
    }

    public static Date dateMinusMinute(Date src, int n) {
        return add(src, ~n+1, "mi");
    }

    public static Date datePlusSecond(Date src, int n) {
        return add(src, n, "s");
    }

    public static Date dateMinusSecond(Date src, int n) {
        return add(src, ~n+1, "s");
    }

    private void testTimeZone(){
        String pattern = PATTERN_SSZ;
        Date now = nowDate();
        System.out.println(now);
        Date now1 = dateMinusMinute(now,60);
        System.out.println(now1);

        String strLocalGMT8 = nowStr(pattern,ZoneId.of("GMT+8"));
        System.out.println("GMT+8:"+strLocalGMT8);

        String strLocal = nowStr(pattern,ZoneId.of("GMT+9"));
        System.out.println("GMT+9:"+strLocal);

        String strUTC = nowStrUTC(pattern);
        System.out.println("UTC:"+strUTC);
    }

    public static void main(String[] args) {
        String pattern = "yyyy-MM-dd'T'HH:mm:ssX";
        String str = "2023-11-02T16:35:02+0800";
        Date d = null;
        try {
            d = stringToDate(str,pattern, ZoneId.of("GMT+8"));
            System.out.println(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println(zonedDateTime);

        ZonedDateTime z1 = ZonedDateTime.parse(str, DateTimeFormatter.ofPattern(pattern));
        System.out.println(z1);

        String nowStrUTC = nowStr(PATTERN_SSS,ZoneId.of("GMT"));
        System.out.println(nowStrUTC);


        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_SS);
        sdf.setTimeZone(TimeZone.getTimeZone( ZoneId.of("UTC")));
        String nowUtcStr = sdf.format(now);
        System.out.println("nowUtcStr:"+nowUtcStr);

        Date tl = new Date(1700117807979L);
        System.out.println(dateToStr(tl,PATTERN_SSS,ZoneId.of("UTC")));
        System.out.println(dateToStr(tl,PATTERN_SSS,ZoneId.of("GMT+8")));
        System.out.println(now.getTime()+"ms");
        System.out.println(now.getTime()/1000 +"s");
    }
}
